package com.longcai.cm.controller;

import com.alibaba.fastjson.JSON;
import com.longcai.cm.base.aop.Log;
import com.longcai.cm.base.bean.PageVo;
import com.longcai.cm.base.bean.Pageable;
import com.longcai.cm.bean.OptionsUtil;
import com.longcai.cm.bean.R;
import com.longcai.cm.common.utils.RequestUtil;
import com.longcai.cm.common.utils.StringUtil;
import com.longcai.cm.domain.JaServiceInfo;
import com.longcai.cm.domain.JaShop;
import com.longcai.cm.service.JaServiceInfoService;
import com.longcai.cm.service.JaShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author LBX
 * @date 2024-07-30
 */
@RestController
@RequestMapping("/JaServiceInfo")
public class JaServiceInfoController {

    private final JaServiceInfoService jaServiceInfoService;

    private final JaShopService jaShopService;

    public JaServiceInfoController(JaServiceInfoService jaServiceInfoService,JaShopService jaShopService) {
        this.jaServiceInfoService = jaServiceInfoService;
        this.jaShopService = jaShopService;
    }

    @RequestMapping("/getList")
    public R<PageVo<JaServiceInfo>> getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JaServiceInfo> result = jaServiceInfoService.getPageBean(pageInfo, JaServiceInfo.class);
        return R.success(result);
    }


    @RequestMapping("/getOptions")
    public R<List<OptionsUtil>> getOptions(){
        List<OptionsUtil> vo = new ArrayList<>();
        List<JaServiceInfo> list = jaServiceInfoService.lambdaQuery()
                .eq(JaServiceInfo::getPid,"0")
                .list();
        for (JaServiceInfo jaServiceInfo : list) {
            OptionsUtil selectVo = new OptionsUtil(jaServiceInfo.getId(),jaServiceInfo.getName());
            vo.add(selectVo);
        }
        return R.success(vo);
    }

    @RequestMapping("/getOptionsList")
    public R<List<OptionsUtil>> getOptionsList(String id){
        List<OptionsUtil> vo = new ArrayList<>();
        if (StringUtils.isBlank(id)){
            return R.error("参数为空");
        }
        String[] split = id.split(",");
        List<String> idList = Arrays.asList(split);
        List<JaServiceInfo> list = jaServiceInfoService.lambdaQuery()
                .in(JaServiceInfo::getPid,idList)
                .list();
        for (JaServiceInfo jaServiceInfo : list) {
            OptionsUtil selectVo = new OptionsUtil(jaServiceInfo.getId(),jaServiceInfo.getName());
            vo.add(selectVo);
        }
        return R.success(vo);
    }

    /**
     * @param vo 插入实体
     * @return R
     */
    @Log
    @RequestMapping("/save")
    public R<String> save(JaServiceInfo vo) {
        if (StringUtils.isNotBlank(vo.getId())) {
            boolean success = jaServiceInfoService.updateById(vo);
            return success ? R.success() : R.error();
        } else {
            final String uuid = StringUtil.getUuid();
            vo.setId(uuid);
            boolean success = jaServiceInfoService.save(vo);
            return success ? R.success() : R.error();
        }
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return R
     */
    @Log
    @RequestMapping("/edit")
    public R<String> edit(JaServiceInfo vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            boolean success = jaServiceInfoService.updateById(vo);
            return success ? R.success() : R.error();
        } else {
            return R.error("id不能为空");
        }
    }

    /**
     * @param id 主键
     * @return R
     */
    @Log
    @RequestMapping("/delete/{id}")
    public R<String> deleteByPrimaryKey(@PathVariable("id") Integer id) {
        final boolean success = jaServiceInfoService.removeById(id);
        return success ? R.success() : R.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return R
     */
    @Log
    @RequestMapping("/delete")
    public R<String> delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            final boolean success = jaServiceInfoService.removeById(idString);
            if (!success) {
                return R.error();
            }
        }
        return R.success();
    }


    @RequestMapping("/goodsOptions")
    public R<String> goodsOptions(String id){
        List<OptionsUtil> optionsUtilList = new ArrayList<>();
        JaShop shop = jaShopService.getById(id);
        if (StringUtils.isNotBlank(shop.getServiceInfoItem())){
            String[] split = shop.getServiceInfoItem().split(",");
            List<JaServiceInfo> list = jaServiceInfoService.lambdaQuery().in(JaServiceInfo::getName, Arrays.asList(split)).list();

            for (JaServiceInfo jaServiceInfo : list) {
                OptionsUtil optionsUtil = new OptionsUtil(jaServiceInfo.getId(), jaServiceInfo.getName());
                optionsUtilList.add(optionsUtil);
            }
        }
        String s = JSON.toJSONString(optionsUtilList);
        return R.success(s);
    }
}
